<?php

class My_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{

    /**
     * This function is called once after router shutdown. It automatically
     * sets the layout for the default MVC or a module-specific layout. If
     * you need to set a custom layout based on the controller called, you
     * can set it here using a switch based on the action or controller or
     * set the layout in the controller itself.
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $layout = Zend_Layout::getMvcInstance();

        $module     = strtolower($request->getModuleName());
        $controller = strtolower($request->getControllerName());
        $action     = strtolower($request->getActionName());

        if ($module !== '') {
            $layout->setLayoutPath(APPLICATION_PATH . '/modules/'
                . $module . '/layouts/scripts/');
            $layout->setLayout($module);
        }
    }

}